package com.jp.knowledge.logic;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.experience.TiyanApps;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/2/6.
 */

public class ExperienceLogic implements ResultSubscriber.OnResultListener {

    private static final int GROUP_CREATE_CODE = 0x01;
    private static final int GROUP_RENAME_CODE = 0x02;
    private static final int ICON_MOVE_IN_CODE = 0x03;
    private static final int ICON_MOVE_OUT_CODE = 0x04;
    private static final int DATA_DELETE_CODE = 0x05;
    private static final int DATA_SORT_CODE = 0x06;

    private Context context;
    private Gson gson;
    private JsonParser jsonParser;

    public ExperienceLogic(Context context){
        this.context = context;
        this.gson = new Gson();
        this.jsonParser = new JsonParser();
    }

    /* 组创建 */
    public void groupCreated(final List<TiyanApps> groupData) {
        if(groupData == null || groupData.size() == 0)
            return;
        final String groupName = groupData.get(0).getGName();
        if(groupName == null || groupName.trim().length() == 0)
            return;
        JsonObject param = new JsonObject();
        param.addProperty("name", groupName);
        HttpHelper.getInstance(context).expAddGroup(param, GROUP_CREATE_CODE, new ResultSubscriber.OnResultListener() {
            @Override
            public void onStart(int requestType) {}
            @Override
            public void onCompleted(int requestType) {}
            @Override
            public void onError(int requestType) {}
            @Override
            public void onNext(IModel data, int requestType) {
                if(data.getErrcode() != 0)
                    return;
                JsonObject jsonObject = JsonUtil.getInstance().entityToJson(data.getData());
                String mid = jsonObject.get("mid").getAsString();
                String gid = jsonObject.get("gid").getAsString();
                if(mid != null && mid.trim().length() > 0 && gid != null && gid.trim().length() > 0){
                    List<String> groupDataIds = new ArrayList<>();
                    for(TiyanApps app : groupData){
                        app.setParentMid(mid);
                        app.setGid(gid);
                        groupDataIds.add(app.getMid());
                    }
                    iconsMoveIn(groupDataIds, gid);
                }
            }
        });
    }

    /* 组重命名 */
    public void groupRename(String name, String groupId) {
        if(name == null || name.trim().length() == 0 || groupId == null || groupId.trim().length() == 0)
            return;
        JsonObject param = new JsonObject();
        param.addProperty("name", name);
        param.addProperty("gid", groupId);
        HttpHelper.getInstance(context).expMyGroupRename(param, GROUP_RENAME_CODE, this);
    }

    /* 应用入组 */
    public void iconsMoveIn(List<String> iconsId, String groupId) {
        iconMoveGroup(iconsId, groupId, true);
    }

    /* 应用出组 */
    public void iconsMoveOut(List<String> iconsId, String groupId) {
        iconMoveGroup(iconsId, groupId, false);
    }

    /* 应用删除 */
    public void dataDelete(List<String> mids) {
        if(mids == null || mids.size() == 0)
            return;
        JsonObject param = new JsonObject();
        JsonArray array = new JsonArray();
        for(String mid : mids)
            array.add(mid);
        param.add("ids", array);
        HttpHelper.getInstance(context).expMyDelete(param, DATA_DELETE_CODE, this);
    }

    /* 应用排序 */
    public void dataSorted(List<List<TiyanApps>> data) {
        if(data == null || data.size() == 0)
            return;
        List<SortBean> sortApps = new ArrayList<>();
        for(List<TiyanApps> groupData : data){
            SortBean sortBean = new SortBean();
            if(groupData == null || groupData.size() == 0)
                continue;
            if (groupData.size()==1) {
                sortBean.setMid(groupData.get(0).getMid());
            } else {
                sortBean.setMid(groupData.get(0).getParentMid());
                List<SortBean> groupSortBeans = new ArrayList<>();
                for(TiyanApps groupApp : groupData){
                    SortBean groupSortBean = new SortBean();
                    groupSortBean.setMid(groupApp.getMid());
                    groupSortBeans.add(groupSortBean);
                }
                sortBean.setApps(groupSortBeans);
            }
            sortApps.add(sortBean);
        }
//        JsonArray jsonArray = new Gson().toJsonTree(sortApps, new TypeToken<List<SortBean>>() {}.getType()).getAsJsonArray();
        JsonObject param = new JsonObject();
        param.add("apps", gson.toJsonTree(sortApps));
        HttpHelper.getInstance(context).expMySort(param, DATA_SORT_CODE, this);
    }

    private void iconMoveGroup(List<String> iconsId, String groupId, boolean isIn){
        if(iconsId == null || iconsId.size() == 0 || groupId == null || groupId.trim().length() == 0)
            return;
        JsonObject param = new JsonObject();
        JsonArray array = new JsonArray();
        for(String iconId : iconsId)
            array.add(iconId);
        param.add("ids", array);
        param.addProperty("gid", groupId);
        param.addProperty("op", isIn);
        HttpHelper.getInstance(context).expMyGroupAdd(param, isIn ? ICON_MOVE_IN_CODE : ICON_MOVE_OUT_CODE, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {}
    @Override
    public void onNext(IModel data, int requestType) {
        L.i(data.toString());
        if(data.getErrcode() != 0)
            return;
        if(requestType == DATA_SORT_CODE)
            LocalBroadcastManager.getInstance(context)
                    .sendBroadcast(new Intent(LocalBroadCast.GET_MY_EXPERIENCE_CODE));
    }

    private class SortBean{
        private String mid;
        private List<SortBean> apps;
        public String getMid() {
            return mid;
        }
        public void setMid(String mid) {
            this.mid = mid;
        }
        public List<SortBean> getApps() {
            return apps;
        }
        public void setApps(List<SortBean> apps) {
            this.apps = apps;
        }
    }
}
